package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

@RestController
public class InputController {
    @Autowired
    private InputRepository inputRepository;
    @Autowired
    private PayPhaseRepository payPhaseRepository;
    @Autowired
    private ClientRepository clientRepository;
    @Autowired
    private ContractRepository contractRepository;
    @PostMapping("/addInput")
    public Result addInput(@RequestBody Input input,
                           HttpServletRequest req){
        input.setId(null);
        HttpSession session = req.getSession();
        Long userid = (Long) session.getAttribute("userid");
        input.setUserid(userid);
        inputRepository.save(input);
        //阶段欠款修改
        Long contractid = input.getContractid();
        List<PayPhase> res = payPhaseRepository.findAllByContractid(String.valueOf(contractid));
        Collections.sort(res, new Comparator<PayPhase>() {
            @Override
            public int compare(PayPhase p1, PayPhase p2) {
                return CharSequence.compare(p1.getOughttime(), p2.getOughttime());
            }
        });
        Double price = input.getInputprice();
        Double allPay = 0.0;
        for(PayPhase p : res){
            Double needpay = p.getOughtpay() - p.getPaypay();
            if(needpay <= price){
                p.setPaypay(p.getOughtpay());
                p.setPaytime(input.getPaytime());
                price-=needpay;
                allPay+=needpay;
            }
            else{
                p.setPaypay(price);
                p.setPaytime(input.getPaytime());
                allPay+=price;
                price = 0.0;
                break;
            }
        }
        for(PayPhase p : res) payPhaseRepository.save(p);
        //公司欠款应该减少
        Long clientid = contractRepository.getClientId(contractid);
        Optional<ClientCompany> clientCompanydata = clientRepository.findById(clientid);
        ClientCompany clientCompany = clientCompanydata.get();
        clientCompany.setOwed(clientCompany.getOwed()-allPay);
        clientRepository.save(clientCompany);
        return Result.success();
    }
    @PostMapping("/getInputs")
    public Result<PageBean<Input>> getInputs(
            @RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        PageBean<Input> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Input> page = inputRepository.getPayPhases(str,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        return Result.success(pb);
    }
    @DeleteMapping("/deleteInput")
    public Result deleteInput(String id){
        System.out.println(id);
        inputRepository.deleteById(Long.valueOf(id));
        return Result.success();
    }
}
